//
//  WBHomeViewController.swift
//  MyWeiBo
//
//  Created by 俞存旭 on 16/9/23.
//  Copyright © 2016年 俞存旭. All rights reserved.
//

import UIKit
//定义全局变量 尽量使用private修饰，否则到处都可以访问
private let cellId = "cellId"


class WBHomeViewController: WBBaseViewController {

    //微博数据数组
    lazy var statusList = [String]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

    }
    //加载数据
    override func loadData() {
        print("开始加载")

        //模拟延时加载数据  dispatch_after
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            
            for i in 0..<15 {
                if self.isPullup {
                    
                    //上拉刷新，
                    self.statusList.append("上拉\(i)")

                } else {
                    //下拉刷新
                    self.statusList.insert(i.description, at: 0)

                }
            }
            print("刷新表格")
            //结束刷新
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            //刷新表格
            self.tableView?.reloadData()
        }
        

    }
    
    
    func showFriends() {
        
        let vc = WBDemoViewController()
//        vc.hidesBottomBarWhenPushed = true
        //push的动作是nav在做
        navigationController?.pushViewController(vc, animated: true)
        
    }

}
//MARK: - 表格数据源方法
extension WBHomeViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //3.返回cell
        return cell
        
    }
}


extension WBHomeViewController {
    
    override func setupTableView() {
     
        super.setupTableView()
        //重构 UIBarButtonItem
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
    
}











